#include "widget.h"
#include "ui_widget.h"

#include <QMovie>
#include <QMouseEvent>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setLayout(ui->verticalLayout);
    this->setFixedSize(this->size());
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->installEventFilter(this);

    abort_flag = false;

    quit = new QMenu(this);
    quit->addAction(QIcon(QPixmap(":/image/Close.png")), tr("Quit"));
    connect(quit, &QMenu::triggered, [=](){this->close();});

    doubao = new wxapi(this);
    connect(doubao, &wxapi::OnScreenDisSig, this, &Widget::display);

    recordMovie = new QMovie(":/image/yinpin.gif");
    recordMovie->setCacheMode(QMovie::CacheAll);
    recordMovie->jumpToFrame(0);

    loadMovie = new QMovie(":/image/send.gif");
    loadMovie->setCacheMode(QMovie::CacheAll);
    loadMovie->jumpToFrame(0);
    ui->label_4->setMovie(loadMovie);
    loadMovie->start();

    voice_rec = new voiceRec(this);
    connect(voice_rec, &voiceRec::send_audioData, [=](QByteArray pcmData){
        emit send_pcmData_to_doubao(pcmData);
    });
    connect(this, &Widget::send_pcmData_to_doubao, doubao, &wxapi:: post_pcmData_to_baidu);

    connect(doubao, &wxapi::reply_abort_sig, [=](){abort_flag = true;});
    timer.setInterval(1000);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(this->isEnabled() || (abort_flag == true))
    {
        abort_flag = false;
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event);
            if(mouse->button() == Qt::LeftButton)
            {
                voice_rec->startRecording();
                mouseStatus = true;
                posOffset = mouse->globalPos() - this->pos();
                ui->label->setMovie(recordMovie);
                recordMovie->start();
                doubao->init_audio_play();
                return true;
            }
            else if(mouse->button() == Qt::RightButton)
            {
                mouseStatus = false;
                quit->exec(mouse->globalPos());
                return true;
            }
        }
        else if(event->type() == QEvent::MouseMove)
        {
            QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event);
            if(mouseStatus == true)
            {
                this->move(mouse->globalPos() - posOffset);
                return true;
            }
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouse = dynamic_cast<QMouseEvent*>(event);
            if(mouse->button() == Qt::LeftButton)
            {
                voice_rec->stopRecording();
                recordMovie->jumpToFrame(0);
                recordMovie->stop();

                loadMovie->stop();
                loadMovie->setFileName(":/image/load.gif");
                loadMovie->jumpToFrame(0);
                ui->label_4->setMovie(loadMovie);
                loadMovie->start();
                this->setEnabled(false);
                return true;
            }
        }
    }
    return QWidget::eventFilter(watched, event);
}

void Widget::display(QString text)
{
    ui->label_2->clear();
    ui->label_2->setText(text);

    loadMovie->stop();
    loadMovie->setFileName(":/image/send.gif");
    loadMovie->jumpToFrame(0);
    ui->label_4->setMovie(loadMovie);
    loadMovie->start();
    this->setEnabled(true);
}

